// 这句的意思就是引入 `express` 模块，并将它赋予 `express` 这个变量等待使用。
var express = require('express');
var superagent = require('superagent');
var cheerio = require('cheerio');
var port = 1630;
var app = express();
var def_config = {
    'referer': 'http://music.163.com',
    'appver': '2.0.2'
};
var origin = 'http://music.163.com';
var _home_page;

app.use(express.static('web'));

app.get('/', function (req, res) {
    res.sendfile('web/index.html');
});

app.listen(port, function () {
    console.log('app is listening at port ', port);
});

//搜索
app.get('/music/search', function (req, res, next) {
    console.log('req type', req.param('type'));
    superagent.post('http://music.163.com/api/search/get/')
        .set(def_config)
        .query({
            's': req.param('s'),
            'limit': req.param('limit') || 20,
            'sub': false,
            'type': req.param('type') || 1
        })
        .end(function (err, sres) {
            if (err) {
                return next(err);
            }
            res.send(sres.text);
        });
});

//获取歌手列表
app.get('/music/albums/artis_id', function (req, res) {
    superagent.get()
        .set(def_config)
        .end();
});

//歌曲信息
app.get('/music/detail', function (req, res) {
    var id = req.param('id');
    superagent.get('http://music.163.com/api/song/detail/')
        .query('id=' + id + '&ids=%5B' + id + '%5D&csrf_token=')
        .end(function (err, sres) {
            res.send(sres);
        });
});

//歌手专辑
app.get('/music/albums', function (req, res) {
    var _id = req.param('id');
    superagent.get('http://music.163.com/api/artist/albums/' + _id)
        .query('id=' + _id + '&offset=0&total=true&limit=5')
        .end(function (err, sres) {
            res.send(sres);
        });
});

//专辑信息
app.get('/music/album', function (req, res) {
    var _id = req.param('id');
    superagent.get('http://music.163.com/api/album/' + _id)
        .query('ext=true&id=' + _id + '&offset=0&total=true&limi')
        .end(function (err, sres) {
            res.send(sres);
        });
});

//歌单
app.get('/music/playlist/detail', function (req, res) {
    var _id = req.param('id');
    superagent.get('http://music.163.com/api/playlist/detail')
        .query('id=' + _id + '&updateTime=-1')
        .end(function (err, sres) {
            res.send(sres);
        });
});

//歌词
app.get('/music/song', function (req, res) {
    var _id = req.param('id');
    superagent.get('http://music.163.com/api/song/lyric')
        .query('os=pc&id=' + _id + '&lv=-1&kv=-1&tv=-1')
        .end(function (err, sres) {
            res.send(sres);
        });
});

//mv
app.get('/music/mv', function (req, res) {
    var _id = req.param('id');
    superagent.get('http://music.163.com/api/mv/detail')
        .query('id=' + _id + '&type=mp4')
        .end(function (err, sres) {
            res.send(sres);
        });
});

//推荐
app.get('/music/discover', function (req, res) {
    superagent.get('http://music.163.com/discover/')
        .end(function (err, sres) {
            let $ = cheerio.load(sres.text);
            let dm = $('#discover-module');
            let cover_list = $('.m-cvrlst > li', '#discover-module');
            var items = [],
                _img, discover = {};
            cover_list.each(function (index, elm) {
                elm = $(elm);
                items.push({
                    img: $('img', elm).attr('src'),
                    title: $('a', elm).attr('title'),
                    id: $('a', elm).data('res-id'),
                    nb: $('.nb', elm).text()
                });
            });
            discover.hot = items;
            discover.new_album = [];
            discover.top_list = [];
            let albums = '', _title = '';
            $('#album-roller li', '#discover-module').each(function (i, elm) {
                elm = $(elm);
                _title = $('.msk', elm).attr('title');
                if (albums.indexOf(_title) > -1)
                    return;
                albums += ',' + _title;
                discover.new_album.push({
                    img: $('.j-img', elm).data('src'),
                    title: _title,
                    id: $('.icon-play', elm).data('res-id'),
                    singer: $('.s-fc3', elm).text()
                });
            });
            // $('#top-flag blk','#discover-module').each(function(i, blk_elm){
            //     let _top = {
            //         title:$('.f-thide', blk_elm).text(),
            //         href:$('.f-thide', blk_elm).parent().attr('href'),
            //         id:''
            //         top10:[]
            //     };

            //     discover.top_list.push(_top);
            // });
            res.send(discover);
        });
});

//分类
app.get('/music/playlist', function (req, res) {
    var _cat = req.param('cat');
    superagent.get('http://music.163.com/discover/')
        .query('cat=' + _cat)
        .end(function (err, sres) {
            res.send(sres);
        });
});

//歌手
app.get('/artist', function (req, res) {
    superagent.get('http://music.163.com/#/artist')
        .query('id=' + req.param('id'))
        .end(function (err, sres) {
            res.send(sres);
        });
});